package com.rchat.pocmini.util;

import com.aso.common.encrypt.AESUtils;
import com.itsdf07.alog.ALog;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by ljd on 2017/2/13.
 * <p>
 * http请求工具类
 */

public class HttpUtil {

    public static final String HTTP_POST = "POST";
    public static final String HTTP_GET = "GET";
    public static final String HTTP_PUT = "PUT";
    public static final String HTTP_DELETE = "DELETE";


    /**
     * http访问
     *
     * @param urlStr      访问的url
     * @param requestJson 参数json格式的String
     * @param method      post，get，etc
     * @return
     * @throws IOException
     */
    public static String httpConnect(String urlStr, String requestJson, String method) {
        //建立连接
        StringBuffer buffer;
        InputStream inputStream = null;
        InputStreamReader inputStreamReader = null;
        BufferedReader bufferedReader = null;
        HttpURLConnection conn = null;
        try {
            URL url = new URL(urlStr);
            conn = (HttpURLConnection) url.openConnection();

            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            conn.setRequestMethod(method);
            conn.setConnectTimeout(10000);//5秒连接超时时间
            conn.setReadTimeout(10000);//5秒读取超时时间
            conn.setRequestProperty("Content-Type", "application/json"); //指定格式为json
            conn.connect();
//        Log.e("response code",conn.getResponseCode() + "");

            if (null != requestJson) {
                OutputStream outputStream = conn.getOutputStream();
                //注意编码格式
                outputStream.write(AESUtils.getInstance().encrypt(requestJson).getBytes("UTF-8"));
                outputStream.flush();
                outputStream.close();
            }
            inputStream = conn.getInputStream();
            inputStreamReader = new InputStreamReader(inputStream);
            bufferedReader = new BufferedReader(inputStreamReader);
            String str = null;
            buffer = new StringBuffer();
            while ((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
            }
        } catch (Exception e) {
            e.printStackTrace();
            //TODO 暂时先返回空字符串，不要返回null，否则json解析会崩溃。后续需要改掉http这一块
            return "";
        } finally {
            //释放资源
            try {
                if (null != bufferedReader) {
                    bufferedReader.close();
                }
                if (null != inputStreamReader) {
                    inputStreamReader.close();
                }
                if (null != inputStream) {
                    inputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (null != conn) {
                conn.disconnect();
            }
        }
        String result = buffer.toString();
        try {
            result = AESUtils.getInstance().decrypt(buffer.toString());
        } catch (Exception e) {
            ALog.eTag("dfsu", "e = %s", e.toString());
        }
        return result;
    }
}
